package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.nls.service.facade.PubSysInfoService;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.nls.common.RiskColltConstant;
import com.fdb.efp.nls.service.dao.CaseDistrRatioDao;
import com.fdb.efp.nls.service.domain.CaseDistrRatio;
import com.fdb.efp.nls.service.facade.CaseDistrRatioFlowService;
import com.fdb.efp.nls.service.vo.CaseDistrRatioVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 
  * 
  * @author liuty
  * @date 2019年9月12日 下午3:59:54
  * @version 1.0
 */
@Service("caseDistrRatioFlowService")
public class CaseDistrRatioFlowServiceImpl implements CaseDistrRatioFlowService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(CaseDistrRatioFlowServiceImpl.class);

    @Autowired
    private CaseDistrRatioDao caseDistrRatioDao;

    @Autowired
    @Qualifier("pubSysInfoService")
    private PubSysInfoService pubSysInfoService;
    @Autowired
    @Qualifier("bizEventInfoService")
    private BizEventInfoService bizEventInfoService;

    @Override
    public void pass(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("案件比例分配通过版本号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                CaseDistrRatioVO vo = new CaseDistrRatioVO();
                vo.setVersionNo(bizSerno);
                List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryByVersionNoByPage(vo);
                for (CaseDistrRatio item : caseDistrRatios) {
                    if (Objects.nonNull(vo)) {
                        // 审批用户编号
                        item.setApprvUserCode(pageApproveVo.getAprvUserId());
                        // 审批意见
                        item.setApprvComment(pageApproveVo.getAprvComment());
                        // 审批状态 = 通过
                        item.setApprvSts(ConsoleConstant.APRV_STATUS_03);
                        // 审批时间
                        item.setApprvFnshTime(TimeUtil.getCurrentDate());
                        item.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        item.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        item.setVersionSts(RiskColltConstant.VERSION_STATUS_02);
                    }
                    caseDistrRatioDao.updateByPk(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("案件比例分配拒绝流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                CaseDistrRatioVO vo = new CaseDistrRatioVO();
                vo.setVersionNo(bizSerno);
                List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryByVersionNoByPage(vo);
                for (CaseDistrRatio item : caseDistrRatios) {
                    if (Objects.nonNull(vo)) {
                        // 审批用户编号
                        item.setApprvUserCode(pageApproveVo.getAprvUserId());
                        // 审批意见
                        item.setApprvComment(pageApproveVo.getAprvComment());
                        // 审批状态 = 拒绝
                        item.setApprvSts(ConsoleConstant.APRV_STATUS_04);
                        // 审批时间
                        item.setApprvFnshTime(TimeUtil.getCurrentDate());
                        item.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        item.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        item.setVersionSts(RiskColltConstant.VERSION_STATUS_03);
                    }
                    caseDistrRatioDao.updateByPk(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
        try {
            if (bizEventInfoService.isFirstNode(bizSerno)) {
                logger.info("案件比例分配打回流水号" + bizSerno);
                if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                	CaseDistrRatioVO vo = new CaseDistrRatioVO();
                    vo.setVersionNo(bizSerno);
                    List<CaseDistrRatio> caseDistrRatios = caseDistrRatioDao.queryByVersionNoByPage(vo);
                    for (CaseDistrRatio item : caseDistrRatios) {
                        if (Objects.nonNull(vo)) {
                            // 审批用户编号
                            item.setApprvUserCode(pageApproveVo.getAprvUserId());
                            // 审批意见
                            item.setApprvComment(pageApproveVo.getAprvComment());
                            // 审批状态 = 打回
                            item.setApprvSts(ConsoleConstant.APRV_STATUS_05);
                            // 审批时间
                            item.setApprvFnshTime(TimeUtil.getCurrentDate());
                            item.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                            item.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        }
                        caseDistrRatioDao.updateByPk(item);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
